package my.algorithms.mcorrea.topcoder;

/**
 * http://community.topcoder.com/stat?c=problem_statement&pm=10943
 * @author mcorrea
 */
public class MissingParentheses {

    public static void main(String[] args) {
		System.out.println( countCorrections("(()(()"));
		System.out.println( countCorrections("()()(()"));
		System.out.println( countCorrections("(())(()())"));
		System.out.println( countCorrections("())(())((()))))()((())))()())())())()()()"));
		System.out.println( countCorrections("))))))))))))))))))))))))))))))))))))))))))))))))))"	));
		System.out.println( countCorrections(")))))))))))))))))))))))))((((((((((((((((((((((((("));
    }

	
	public static int countCorrections(String par){
		int count = 0;
		int countOpen = 0;
		int countClose = 0;
		for(char c: par.toCharArray()){
			if(c == '('){
				countOpen++;
			}else{
				if(c == ')'){
				countClose++;
				}
			}
			
			if(countClose > countOpen){
				count+= countClose - countOpen;
				countOpen = countClose;
			}
		}
		 
		count += countOpen - countClose;
		return count;
	}
	
	
}
